//
//  HDCFModifyPhoneVc.m
//  HaoDeCaiFu
//
//  Created by 郑开宏 on 2017/11/15.
//  Copyright © 2017年 郑开宏. All rights reserved.
//

#import "HDCFModifyPhoneVc.h"
#import "JKCountDownButton.h"
#import "SVProgressHUD.h"

@interface HDCFModifyPhoneVc ()
@property (weak, nonatomic) IBOutlet UITextField *phoneOne;
@property (weak, nonatomic) IBOutlet UITextField *phoneTwo;
@property (weak, nonatomic) IBOutlet UITextField *phoneCode;
@property (weak, nonatomic) IBOutlet JKCountDownButton *jkBtn;
@property (weak, nonatomic) IBOutlet UIButton *comitBtn;

@end

@implementation HDCFModifyPhoneVc

- (IBAction)jkBtnClick:(JKCountDownButton *)sender {
    if (_phoneTwo.text.length < 1) {
        [SVProgressHUD showInfoWithStatus:@"请输入新手机号"];
        [SVProgressHUD dismissWithDelay:1.0];
        return;
    }
   [sender startWithSecond:60];
    [SVProgressHUD show];
    NSDictionary *dict = @{
                           @"Phone":self.phoneTwo.text,
                           @"Type":@0,
                           @"CheckExistentOrNo":@"false"
                           };
    NSString *jsonString = [self dictionaryToJson:dict];
    [HttpRequest sendSelfPostRequestWithURL:@"api/SiteConfig/RequestPhoneCode" WithParamets:jsonString WithParamet:nil success:^(id result) {
        if ([result[@"code"] isEqual:@1]) {
            
            [SVProgressHUD showSuccessWithStatus:@"发送成功"];
            [SVProgressHUD dismissWithDelay:1.0];
        }else{
            [SVProgressHUD showInfoWithStatus:result[@"msg"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"服务器错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}
- (IBAction)commitBtnClick:(UIButton *)sender {
    if (_phoneTwo.text.length < 1) {
        [SVProgressHUD showInfoWithStatus:@"请输入新手机号"];
        [SVProgressHUD dismissWithDelay:1.0];
        return;
    }
    if (_phoneCode.text.length < 1) {
        [SVProgressHUD showInfoWithStatus:@"请输入验证码"];
        [SVProgressHUD dismissWithDelay:1.0];
        return;
    }
    [SVProgressHUD show];
    NSDictionary *dict = @{
                           @"NewPhone":self.phoneTwo.text,
                           @"CheckCode":self.phoneCode.text
                           };
    NSString *jsonString = [self dictionaryToJson:dict];
    [HttpRequest sendSelfPostRequestWithURL:@"api/User/ModifyPhone" WithParamets:jsonString WithParamet:nil success:^(id result) {
        if ([result[@"code"] isEqual:@1]) {
            [SVProgressHUD showSuccessWithStatus:@"修改成功"];
            [SVProgressHUD dismissWithDelay:1.0];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
                if ([self.delegate respondsToSelector:@selector(modifyPhoneSucess)]) {
                    [self.delegate modifyPhoneSucess];
                }
            });
            
        }else{
            [SVProgressHUD showInfoWithStatus:result[@"msg"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"服务器错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}
- (NSString *)dictionaryToJson:(NSDictionary *)dic{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSString *jsonTemp = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    return jsonTemp;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"修改手机号"];
    
    self.comitBtn.layer.cornerRadius = 5.0;
    self.comitBtn.clipsToBounds = YES;
    
    UIBezierPath *maskPath=[UIBezierPath bezierPathWithRoundedRect:self.jkBtn.bounds byRoundingCorners:UIRectCornerTopRight|UIRectCornerBottomRight cornerRadii:CGSizeMake(6, 6)];
    CAShapeLayer *maskLayer=[[CAShapeLayer alloc]init];
    maskLayer.frame=self.jkBtn.bounds;
    maskLayer.path=maskPath.CGPath;
    self.jkBtn.layer.mask=maskLayer;
    self.jkBtn.clipsToBounds = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
